#include "headfiles.h"

using namespace std;

void paintFill(COLOR *area, int i, int j, COLOR old_color, COLOR new_color, int x_range, int y_range) {
    // Out of the range, return directly
    if (i < 0 || 
        i >= x_range ||
        j < 0 ||
        j >= y_range) {
        return;
    }

    // The painted color is the same as before.
    // No need to paint the area, return
    if (old_color == new_color) {
        return;
    }

    int loc = i * y_range + j;
    // Current location is not the old color,
    // It is another area. return.
    if (area[loc] != old_color) {
        return;
    }
    else {
        area[loc] = new_color;
        paintFill(area, i + 1, j, old_color, new_color, x_range, y_range);
        paintFill(area, i - 1, j, old_color, new_color, x_range, y_range);
        paintFill(area, i, j + 1, old_color, new_color, x_range, y_range);
        paintFill(area, i, j - 1, old_color, new_color, x_range, y_range);
    }
}

void printArea(COLOR *area, int x_range, int y_range) {
    int loc, i, j;
    for (i = 0; i < x_range; i++) {
        for (j = 0; j < y_range; j++) {
            int loc = i * y_range + j;
            cout << area[loc] << "\t";
        }
        cout << endl;
    }
    cout << endl;
}

